package thread

import (
	"time"

	"gitee.com/sansaniot/ssiot-core/facade/runtime"
	"gitee.com/sansaniot/ssiot-core/logger"

	"ssalarm/internal/alarm/models"
)

type SyncDevAlarmThread struct {
}

//执行方法
func (SyncDevAlarmThread) Run(threadName string) {
	logger.Infof("启动线程 - %s.", threadName)
	go func() {
		for {
			time.Sleep(60 * time.Second)
			syncAlarmStatus()
		}
	}()
}

func syncAlarmStatus() {
	db := runtime.Runtime.GetDbByKey("db")
	if db == nil {
		return
	}
	alarmModel := models.SysAlarm{}
	devModel := models.SysDev{}
	alarmModel.Orm = db
	devModel.Orm = db
	//查询当前正在告警的设备集合
	alarmSns := alarmModel.CurrentAlarmSns()
	//更新设备实时告警状态
	devModel.UpdateAlarmStatus(alarmSns)
}
